/**
 * 云开发快捷登录与用户资料封装
 */
export async function cloudQuickLogin(): Promise<string> {
  if (!(typeof wx !== 'undefined' && wx.cloud)) throw new Error('cloud not initialized')
  const res = await wx.cloud.callFunction({ name: 'login' })
  const openid = (res && res.result && res.result.openid) || ''
  if (!openid) throw new Error('login failed')
  return openid
}

/**
 * 获取用户昵称与头像（需用户授权）
 */
export async function getUserProfile(): Promise<{ nickname: string; avatar: string }> {
  return new Promise((resolve, reject) => {
    if (typeof wx === 'undefined' || !wx.getUserProfile) {
      reject(new Error('getUserProfile not available'))
      return
    }
    wx.getUserProfile({
      desc: '用于完善个人资料',
      success: (res) => {
        const user = res.userInfo || {}
        resolve({ nickname: user.nickName || '', avatar: user.avatarUrl || '' })
      },
      fail: () => reject(new Error('user profile denied'))
    })
  })
}

